package com.wuye.web;

import javax.servlet.http.*;
import org.springframework.stereotype.*;
import org.apache.commons.logging.*;

@Service
public class SessionHandler{
	private Log log = LogFactory.getLog(this.getClass());
	private String sessionName="userSession";
	private String adminSessionName="adminSession";
	public boolean useSessionExists(HttpSession session){
		if(session==null) return false;
		if(session.getAttribute(sessionName)==null) return false;
		return true;
	}

	public boolean adminSessionExists(HttpSession session){
		if(session==null) return false;
		if(session.getAttribute(adminSessionName)==null) return false;
		return true;
	}

	public UserSession getUserSession(HttpSession session){
		if(useSessionExists(session))
			return (UserSession)session.getAttribute(sessionName);
		UserSession userSession=new UserSession();
		session.setAttribute(sessionName,userSession);
		return userSession;
	}
	public AdminSession getAdminSession(HttpSession session){
		if(adminSessionExists(session))
			return (AdminSession)session.getAttribute(adminSessionName);
		AdminSession adminSession=new AdminSession();
		session.setAttribute(adminSessionName,adminSession);
		return adminSession;
	}

	public UserSession getSession(HttpSession session){
		return (UserSession)session.getAttribute(sessionName);
	}
	public void saveUserSession(UserSession userSession,HttpSession session){
		session.setAttribute(sessionName,userSession);
	}
	public void saveAdminSession(AdminSession adminSession,HttpSession session){
		session.setAttribute(adminSessionName,adminSession);
	}
	public void removeSession(HttpSession session){
		session.removeAttribute(sessionName);
		session.invalidate();
	}
}